package com.tubeonfire.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tubeonfire.entity.Mission;
import com.tubeonfire.entity.Playlist;
import com.tubeonfire.entity.Tube;
import com.tubeonfire.model.admin.MissionModel;
import com.tubeonfire.model.admin.PlaylistModel;
import com.tubeonfire.model.admin.TubeModel;
import com.tubeonfire.service.YoutubeService;
import com.tubeonfire.util.IdUniqueHelper;

@SuppressWarnings("serial")
public class AddMissionServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(AddMissionServlet.class
			.getName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		try {
			HttpSession session = req.getSession();
			Mission obj = new Mission();
			session.setAttribute("obj", obj);
			req.getRequestDispatcher("/admin/form_mission.jsp").forward(req,
					resp);
		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
			resp.sendError(4004,
					"We are sorry for the inconvenience ! Please try again later !");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			HttpSession session = req.getSession();
			String type = (String) req.getParameter("type");
			String channelId = (String) req.getParameter("channelId");
			String playlistId = (String) req.getParameter("playlistId");
			Mission mission = new Mission();
			Playlist pl = PlaylistModel.byId(playlistId, false);
			if (pl != null) {
				if (YoutubeService.checkChannel(channelId)) {
					if (type.equals("2")) {
						YoutubeService tubeService = new YoutubeService();
						tubeService.setPage(1);
						tubeService.setLimit(49);
						List<Tube> listToAdd = new ArrayList<Tube>();
						tubeService.searchByChannel(channelId, true);
						for (Tube tube : tubeService.getListResult()) {
							tube.setPlaylistId(playlistId);
							listToAdd.add(tube);
						}
						TubeModel.insert(listToAdd);
						mission.setId(IdUniqueHelper.getId());
						mission.setSearchHd(1);
						mission.setSearchType(1);
						mission.setSearchKeyword(channelId);
						mission.setPlaylistId(playlistId);
						mission.setSearchLimit(tubeService.getLimit());
						if (tubeService.getListResult().size() > 0
								&& !tubeService.isTooManyRecentCallError()) {
							mission.setSearchPage(tubeService.getPage() + 1);
						} else {
							mission.setSearchPage(tubeService.getPage());
						}
						mission.setSearchTotalPage(tubeService.getTotalPage());
						mission.setType(2);
						mission.setTitle("Get videos from channel " + channelId
								+ " to playlist " + pl.getTitle());
						MissionModel.insert(mission);
						session.setAttribute("success", "Action success !");
						resp.sendRedirect("/admin/mission/add");
					} else if (type.equals("1")) {
						mission.setId(IdUniqueHelper.getId());
						mission.setType(1);
						mission.setPlaylistId(playlistId);
						mission.setSearchKeyword(channelId);
						mission.setTitle("Subcribe from channel " + channelId);
						MissionModel.insert(mission);
						session.setAttribute("success", "Action success !");
						resp.sendRedirect("/admin/mission/add");
					}
				} else {
					session.setAttribute(
							"error",
							"Channel \""
									+ channelId
									+ "\" is not exits or have no videos ! Please try another channel Id !");
					resp.sendRedirect("/admin/mission/add");
				}
			} else {
				session.setAttribute("error",
						"Playlist's not exits or has been deleted !");
				resp.sendRedirect("/admin/mission/add");
			}

		} catch (Exception e) {
			log.warning(e.toString());
			e.printStackTrace();
			resp.sendError(4004,
					"We are sorry for the inconvenience ! Please try again later !");
		}
	}
}
